package com.yanze.schoolinformation.viewModule.circle;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.resp.RespBook;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.community.circle.SecondBookActivity;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/5/10.
 */

public class SecondBookViewModel {

    private SecondBookViewNavigator mNavigator;
    private SecondBookActivity mActivity;
    private int mPage = 1;
    private int mPageSize = 10;
    private Disposable mDisposable;

    public SecondBookViewModel(SecondBookActivity mActivity) {
        this.mActivity = mActivity;
    }

    public void setmNavigator(SecondBookViewNavigator mNavigator) {
        this.mNavigator = mNavigator;
    }

    public int getmPage() {
        return mPage;
    }

    public void setmPage(int mPage) {
        this.mPage = mPage;
    }

    public void getAllBook() {
        DataReqo.getInstant().getAllSecondBook(C.url.API_School, mPage, mPageSize)
                .subscribe(new DeforObserver<RespBook>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void _onNext(RespBook entity) {
                        if (mNavigator == null) {
                            return;
                        }
                        mNavigator.showLoadSuccessView();
                        if (mPage == 1) {
                            if (entity != null
                                    && entity.getData() != null
                                    && entity.getData().size() > 0) {
                                mNavigator.onRefreshSuccess(entity);
                            } else {
                                mNavigator.showLoadFailedView();
                            }
                        } else {
                            if (entity != null
                                    && entity.getData() != null
                                    && entity.getData().size() > 0) {
                                mNavigator.onRefreshSuccess(entity);
                            } else {
                                mNavigator.showNoMoreLoading();
                            }
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        if (mNavigator == null) {
                            return;
                        }
                        ToastUtils.show(mActivity, msg);
                        mNavigator.showLoadFailedView();
                        if (mPage > 1) {
                            mPage--;
                        }
                    }
                });
    }


    public void onDestory() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
